<template>
	<div class="system-menu-dialog-container energy-from energy-dialog">
		<el-dialog center align-center v-model="state.dialog.isShowDialog" width="55%">
			<template #header="{ titleClass }">
				<div :class="titleClass">{{ state.dialog.isAdd ? '新增菜单' : '修改菜单' }}</div>
			</template>
			<el-form ref="menuDialogFormRef" :model="state.ruleForm" size="default" label-width="80px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="菜单类型">
							<el-radio-group v-model="state.formData.type">
								<el-radio :label="1">菜单</el-radio>
								<el-radio :label="2">按钮</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="上级菜单">
							<el-cascader
								:options="state.allMenuData"
								:props="{ checkStrictly: true, value: 'id', label: 'title' }"
								placeholder="请选择上级菜单,顶级菜单此项为空(不选)"
								clearable
								class="w100"
								v-model="state.formData.pid"
							>
								<template #default="{ node, data }">
									<span>{{ data.title }}</span>
									<span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
								</template>
							</el-cascader>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item :label="state.formData.type == 1 ? '菜单名称' : '按钮名称'">
							<el-input v-model="state.formData.menuName" clearable placeholder="name值(驼峰命名),如 : systemMenu"></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item :label="state.formData.type == 1 ? '菜单标题' : '按钮标题'">
							<el-input v-model="state.formData.title" clearable placeholder="菜单显示的title,如 : 菜单管理"></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="路由地址">
							<el-input v-model="state.formData.path" placeholder="路由中的 path 值(按钮权限和按钮名称保持一致即可)" clearable></el-input>
						</el-form-item>
					</el-col>
					<template v-if="state.formData.type == 1">
						<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
							<el-form-item label="组件路径">
								<el-input
									v-model="state.formData.component"
									placeholder="component的值(前缀/@/views和/@不写),如: /xxx/index.vue"
									clearable
								></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
							<el-form-item label="重定向">
								<el-input v-model="state.formData.redirect" placeholder="如 : /home" clearable></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
							<el-form-item label="菜单图标">
								<IconSelector placeholder="请选择菜单图标" v-model="state.formData.icon" />
							</el-form-item>
						</el-col>
					</template>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="权限标识">
							<el-input
								v-model="state.formData.perms"
								placeholder="请输入权限标识,如 : intelligentSchedule:operationSchedule:save"
								clearable
							></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="菜单排序">
							<el-input-number v-model="state.formData.sort" controls-position="right" placeholder="请输入排序" class="w100" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="菜单状态">
							<el-switch
								v-model="state.formData.status"
								size="default"
								:active-value="0"
								:inactive-value="1"
								active-text="启用"
								inactive-text="禁用"
								style="--el-switch-on-color: #13ce66"
							/>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="备注">
							<el-input v-model="state.formData.remark" maxlength="20" placeholder="最多可输入20个字符" show-word-limit type="text" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>

			<div class="flex row-center mt10 mb10">
				<div class="btn3" @click="onCancel">
					<el-icon> <ele-Close /> </el-icon><span class="ml6">取 消</span>
				</div>
				<div class="btn1 ml10" @click="onSubmit">
					<el-icon> <ele-Check /> </el-icon><span class="ml6">保 存</span>
				</div>
			</div>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemMenuDialog">
import { defineAsyncComponent, reactive, ref } from 'vue';
import { useMenuApi } from '/@/api/menu';
import { ElNotification } from 'element-plus';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 引入组件
const IconSelector = defineAsyncComponent(() => import('/@/components/iconSelector/index.vue'));

// 定义变量内容
const menuDialogFormRef = ref();
const state: any = reactive({
	dialog: {
		isShowDialog: false,
		isAdd: true,
	},

	// 表单数据
	formData: {
		id: '',
		pid: 0, // 父级菜单id
		component: '', // 组件路径
		icon: '', // 菜单图标
		menuName: '', // 菜单标识 (name)
		path: '', // 路由地址
		title: '', // 菜单名称
		redirect: '', // 重定向路径
		perms: '', // 权限标识
		status: 0, // 菜单状态（0正常 1停用）
		type: 1, // 菜单类型(1菜单 2按钮)
		sort: 0, // 排序号
		remark: '', // 备注
	},
	allMenuData: [], // 上级菜单数据
});

// 打开弹窗
const openDialog = (row?: any) => {
	state.dialog.isShowDialog = true;
	getALLMenuData();
	if (row) {
		state.dialog.isAdd = false;
		state.formData = {
			id: row.id,
			pid: row.pid, // 父级菜单id
			component: row.component, // 组件路径
			icon: row.icon, // 菜单图标
			menuName: row.menuName, // 菜单名称
			title: row.title,
			path: row.path, // 路由地址
			redirect: row.redirect,
			perms: row.perms, // 权限标识
			status: row.status, // 菜单状态（0正常 1停用）
			type: row.type, // 菜单类型(1菜单 2按钮)
			sort: row.sort, // 排序号
			remark: row.remark, // 备注
		};
	} else {
		state.dialog.isAdd = true;
		state.formData = {
			id: '',
			pid: 0, // 父级菜单id
			component: '', // 组件路径
			icon: '', // 菜单图标
			menuName: '', // 菜单名称
			title: '',
			path: '', // 路由地址
			perms: '', // 权限标识
			redirect: '',
			status: 0, // 菜单状态（0正常 1停用）
			type: 1, // 菜单类型(1菜单 2按钮)
			sort: 0, // 排序号
			remark: '', // 备注
		};
		// 清空表单，此项需加表单验证才能使用
		// nextTick(() => {
		// 	menuDialogFormRef.value.resetFields();
		// });
	}
};

// 获取所有权限数据-筛选出菜单
const getALLMenuData = async () => {
	const { data } = await useMenuApi().getAllMenu();
	state.allMenuData = filterMenu(data);
};

// 过滤掉按钮权限
const filterMenu = (row: any[]) => {
	let result: any = [];
	row.forEach((item: any) => {
		if (item.type === 1) {
			if (item.childList && item.childList.length > 0) {
				result.push({
					id: item.id,
					title: item.title,
					children: filterMenu(item.childList),
				});
			} else {
				result.push({
					id: item.id,
					title: item.title,
					children: [],
				});
			}
		}
	});
	return result;
};

// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};

// 取消
const onCancel = () => {
	closeDialog();
};

// 新增
const doAddMenu = async () => {
	state.formData.pid = state.formData.pid == 0 ? state.formData.pid : state.formData.pid[state.formData.pid.length - 1];
	const data = await useMenuApi().addMenu(state.formData);
	if (data.code == 200) {
		ElNotification({
			title: '提示',
			message: '新增菜单成功',
			position: 'top-right',
			type: 'success',
		});
		emit('refresh');
	}
};

// 编辑
const doEditMenu = async () => {
	// state.formData.pid = Number(state.formData.pid[state.formData.pid.length - 1]);
	state.formData.pid = state.formData.pid[state.formData.pid.length - 1];
	const data = await useMenuApi().updateMenu(state.formData);
	if (data.code == 200) {
		ElNotification({
			title: '提示',
			message: '修改菜单成功',
			position: 'top-right',
			type: 'success',
		});
		emit('refresh');
	}
};

// 提交
const onSubmit = () => {
	state.dialog.isAdd ? doAddMenu() : doEditMenu();
	closeDialog(); // 关闭弹窗
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>

<style scoped lang="scss">
:deep(.el-dialog__header) {
	width: 100%;
	height: 60px;
	background-color: #5e8dff;
}

:deep(.el-dialog__header .el-dialog__title) {
	font-size: 20px;
	color: #fff;
}

:deep(.el-dialog__header .el-dialog__close) {
	color: #fff;
}
</style>
